package com.test.cordova.test;

import android.Manifest;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.Log;


/**
 * 监听电话状态
 * Created by Administrator on 2018/6/22 0022.
 */

public class PhoneBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = PhoneBroadcastReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {

        TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
       // telMgr.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
            //如果是去电（拨出）
            Log.e(TAG,"拨出");
        }else{
            //查了下android文档，貌似没有专门用于接收来电的action,所以，非去电即来电
            Log.e(TAG,"来电");
            netChange(telMgr, 2,context);

        }

    }

    private void netChange(TelephonyManager telMgr,int telTypeNum,Context context) {
        switch (telMgr.getCallState()) {
            //来电
            case TelephonyManager.CALL_STATE_RINGING:
                Log.e(TAG, "响铃:来电号码 CALL_STATE_RINGING:" );
                break;
            //响铃
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.e(TAG, "接听 CALL_STATE_OFFHOOK:" );
                break;
            //挂断
            case TelephonyManager.CALL_STATE_IDLE:
                PhoneNetUitl.count++;
                Log.e(TAG, "挂断 CALL_STATE_IDLE:"+  PhoneNetUitl.count);
                if (PhoneNetUitl.count == telTypeNum) {
                    Log.e(TAG, "挂断end");
                    String[] permission = new String[]{Manifest.permission.MODIFY_PHONE_STATE,Manifest.permission.CHANGE_NETWORK_STATE};
                    for (int i = 0; i < permission.length; i++) {
                        int state = ContextCompat.checkSelfPermission(context, permission[i]);
                        if (state == PackageManager.PERMISSION_DENIED) {
                            Log.e(TAG, permission[i]+"未授权");

                        } else if(state == PackageManager.PERMISSION_GRANTED){
                            Log.e(TAG, permission[i]+"授权");
                        }
                    }


                    // PhoneNetUitl.setMobileDataEnable(context,false);
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                   // PhoneNetUitl.setMobileDataEnable(context,true);
                    PhoneNetUitl.count = 0;

                }

                break;
        }
    }

    /*PhoneStateListener listener=new PhoneStateListener(){

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            //state 当前状态 incomingNumber,貌似没有去电的API
            super.onCallStateChanged(state, incomingNumber);
            switch(state){
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("listener 挂断");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("listener 接听");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("listener 响铃:来电号码"+incomingNumber);
                    //输出来电号码
                    break;
            }
        }
    };*/
}
